package cn.com.sxt;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.junit.Test;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;;

/**
 * <p>Title: AuthenticationTest</p>  
 * <p>Description: </p>  
 * @author WangJun
 * @date 2018年9月18日 上午10:50:18
 */
public class AuthenticationTest {
	public static void main(String[] args) {
		// 构建SecurityManager工厂，IniSecurityManagerFactory可以从ini文件中初始化SecurityManager环境
				Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
				//通过工厂获取实例
				SecurityManager instance = factory.getInstance();
				//将securityManager设置到运行环境中
				SecurityUtils.setSecurityManager(instance);
				//获取subject实例
				Subject subject = SecurityUtils.getSubject();
				UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","123456");
				try {
					subject.login(token);
					if(subject.isAuthenticated()) {
						System.out.println("验证成功");
					}

				} catch (AuthenticationException e) {
					System.out.println("验证失败");
					e.printStackTrace();
				}
				subject.hasRole("");
				subject.checkPermission("");
				
	}
	@Test
	public void testAuthentication() throws Exception{
		// 构建SecurityManager工厂，IniSecurityManagerFactory可以从ini文件中初始化SecurityManager环境
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		//通过工厂获取实例
		SecurityManager instance = factory.getInstance();
		//将securityManager设置到运行环境中
		SecurityUtils.setSecurityManager(instance);
		//获取subject实例
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","123456");
		subject.login(token);
		//验证权限是否通过
		subject.isAuthenticated();
		System.out.println(subject.isAuthenticated());
	}
}	
